package com.example.SBootDemo.controller.system;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.SBootDemo.ExceptionHandel.exception.Myexception;
import com.example.SBootDemo.model.echarts.pie.pieData;
import com.example.SBootDemo.model.echarts.pie.pieEcharts;
import com.example.SBootDemo.model.function.function;
import com.example.SBootDemo.model.function.user;
import com.example.SBootDemo.service.Interface.function.funinterface;
import com.example.SBootDemo.service.Interface.function.topmoduleinterface;
import com.example.SBootDemo.service.Interface.function.userinterface;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
 * 集成shiro权限验证，controller可通过：
 * 1.---Subject currentUser = SecurityUtils.getSubject();
 * 2.---currentUser.isPermitted("/user/adduser");
 * 来判断用户是否拥有此url请求权限
 * 现在controller暂未添加此判断以后需要时添加
 * @author 944658413
 *
 */
@Controller
public class LoginController {

	@Autowired
	userinterface uservice;
	@Autowired
	funinterface fun;
	@Autowired
	topmoduleinterface topmodule;
	
	
	//登陆
	 @RequestMapping(value="/login", method=RequestMethod.GET)
	public String Login(HttpServletRequest request,user user){
		
	        return "/login";  
	}
	 @RequestMapping(value="/login", method=RequestMethod.POST)
	public String doLogin(  user user,HttpServletRequest request) throws Myexception{
//		 if(bindingResult.hasErrors()){
//	            return "login";
//	        }

	        String username = user.getUsername();
	        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
	        //获取当前的Subject  
	        
	        Subject currentUser = SecurityUtils.getSubject();  
	        try {  
	            //在调用了login方法后,SecurityManager会收到AuthenticationToken,并将其发送给已配置的Realm执行必须的认证检查  
	            //每个Realm都能在必要时对提交的AuthenticationTokens作出反应  
	            //所以这一步在调用login(token)方法时,它会走到MyRealm.doGetAuthenticationInfo()方法中,具体验证方式详见此方法  
	        	 
	            currentUser.login(token);  
	           
	        }catch(UnknownAccountException uae){  
	            throw new Myexception("未知账户");
	          
	        }catch(IncorrectCredentialsException ice){  
	        	throw new Myexception("密码不正确");
	          
	        }catch(LockedAccountException lae){  
	        	throw new Myexception("账户已锁定");
	             
	        }catch(ExcessiveAttemptsException eae){  
	        	throw new Myexception("用户名或密码错误次数过多");
	            
	        }catch(AuthenticationException ae){  
	            //通过处理Shiro的运行时AuthenticationException就可以控制用户登录失败或密码错误时的情景  
	           
	            ae.printStackTrace(); 
	            throw new Myexception("用户名或密码不正确");
	            
	        }  
	        //验证是否登录成功  
	        
	        if(currentUser.isAuthenticated()){  
	          
	            return "redirect:/index0";
	        }else{  
	            token.clear();  
	            return "redirect:/login";
	        }  
	    
	}
	 
	 @RequestMapping(value="/loginout", method=RequestMethod.GET)
		public String Loginout(HttpServletRequest request,user user){
		 Subject currentUser = SecurityUtils.getSubject();  
		 currentUser.logout();
		        return "/login";  
		}
	
	@RequestMapping(value="/index0", method=RequestMethod.GET)
	
	public String getIndex(HttpServletRequest request){
		
		 request.setAttribute("key", "hello world");  
		 /**
		  * 测试饼图数据
		  */
		 List<pieData> list = new ArrayList<>();
		 List<String> list2 = new ArrayList<>();
		
		 for(int i=0;i<6;i++){
			 list.add(new pieData(20+i,"测试"+i));
			 list2.add("测试"+i);
		 }
		
		 pieEcharts pie = new pieEcharts(list2,"测试的饼图",list);
		 String piestring=null;
		 ObjectMapper mapper = new ObjectMapper();
		 try {
			 piestring=mapper.writeValueAsString(pie);
			System.out.println(piestring);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 //顶层模块筛选
		 request.getSession().setAttribute("topmodule",topmodule.findByUser());
		 request.setAttribute("pie", piestring);
	        return "/index0";  
	}
	
	@RequestMapping(value="/system")
	
	public String getb(HttpServletRequest request,String id) throws Myexception{
		fun.getSystemFun( id,request);
		request.getSession().setAttribute("topmodule",topmodule.findByUser());
		 System.out.println(id);
	        return "/index";  
	}
	

}

